//
//  PhysicalController.m
//  HarmonizePub
//
//  Created by rzhy on 2017/1/21.
//  Copyright © 2017年 Zy. All rights reserved.
//

#import "PhysicalController.h"
#import "Config.h"
#import <JKCountDownButton.h>
#import "SVProgressHUD.h"
#import "Utils.h"
#import "PhysicalRecordController.h"

@interface PhysicalController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *numberField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet JKCountDownButton *codeButton;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;

@end

@implementation PhysicalController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.codeButton.layer.cornerRadius = 5.0f;
    self.codeButton.layer.masksToBounds = YES;
    self.searchButton.layer.cornerRadius = 5.0f;
    self.searchButton.layer.masksToBounds = YES;
    [self.phoneField becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 验证码
- (IBAction)getCodeEvent:(JKCountDownButton *)sender {
    NSString *phoneNum = _phoneField.text;
    NSString *number = _numberField.text;
    if (!phoneNum || [phoneNum isEqualToString:@""])
        [self setAlertViewWithMessage:@"请输入手机号码!"];
    else if (![Utils matchingTelNumber:phoneNum])
        [self setAlertViewWithMessage:@"请输入正确的手机号码!"];
    else if (!number.length)
        [self setAlertViewWithMessage:@"请输入体检编号!"];
    else {
        [self.view endEditing:NO];
        sender.enabled = NO;
        [sender startCountDownWithSecond:60];
        [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.backgroundColor = [UIColor lightGrayColor];
            return [NSString stringWithFormat:@"剩余%lu秒", (unsigned long)second];
        }];
        [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
            countDownButton.enabled = YES;
            countDownButton.backgroundColor = RGBAColor(125, 26, 31, 0.7);
            return @"重新获取";
        }];
        [SVProgressHUD show];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
        NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
        NSDictionary *param = @{@"sjhm" : phoneNum, @"tjbh" : number};
        __weak typeof(self) weakSelf = self;
        [HTTPSTool POST:XHUserCheckTjbrda params:param cookie:cookie success:^(NSDictionary * _Nonnull JSON) {
//            XHLog(@"获取验证码 -> JSON: %@", JSON)；
            [SVProgressHUD dismiss];
            NSInteger result = [JSON[@"ret"] integerValue];
            if (result > 1) {
                [weakSelf setAlertViewWithMessage:JSON[@"msg"]];
                [sender stopCountDown];
            } else if (result == 1) {
                [ZYToast showBottomText:@"验证码发送成功!"];
                weakSelf.searchButton.enabled = YES;
                weakSelf.searchButton.backgroundColor = RGBAColor(125, 26, 31, 0.7);
            } else if (result == -1) {
                // 登录
                [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"XHLogin" object:nil userInfo:nil]];
                [sender stopCountDown];
            } else {
                [sender stopCountDown];
            }
        } failure:^(NSError * _Nonnull error) {
            [SVProgressHUD dismiss];
            [sender stopCountDown];
        }];
    }
}

#pragma mark - 查询
- (IBAction)searchPhysicalEvent {
    NSString *phone = _phoneField.text;
    NSString *number = _numberField.text;
    NSString *code = _codeField.text;
    if (!phone || [phone isEqualToString:@""])
        [self setAlertViewWithMessage:@"请输入手机号码!"];
    else if (![Utils matchingTelNumber:phone])
        [self setAlertViewWithMessage:@"请输入正确的手机号码!"];
    else if (!code || [code isEqualToString:@""])
        [self setAlertViewWithMessage:@"请输入验证码!"];
    else {
        [self.view endEditing:NO];
        NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
        NSDictionary *param = @{@"sjhm" : phone, @"tjbh" : number, @"code" : code};
        __weak typeof(self) weakSelf = self;
        [HTTPSTool POSTRequestWithPath:XHUserGetTjbg cookie:cookie params:param success:^(NSDictionary * _Nonnull JSON) {
//            XHLog(@"获取体检总检报告 -> JSON: %@", JSON);
            [_codeButton stopCountDown];
            NSDictionary *dataDict = JSON[@"data"];
            PhysicalRecordController *record = [[PhysicalRecordController alloc] init];
            record.dataDictionary = dataDict;
            record.tjbh = number;
            [weakSelf.navigationController pushViewController:record animated:YES];
        }];
    }
}

#pragma mark - Alert View
- (void)setAlertViewWithMessage:(NSString *)message {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示:" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:defaultAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - Touch
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:NO];
}

@end
